/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filetransserv;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 *
 * @author Oscar
 */
public class Server {

    private ServerSocket server;
    private int port = 7777;
    private JList Status;
    private int modelSize;
    private String[] StatusString;
    private ListModel model;

    public ServerSocket Server(JList Status) {
        try {
            server = new ServerSocket(port);
            this.Status = Status;
        } catch (IOException e) {
            //e.printStackTrace();
        }
        return server;
    }

    /**
     * Maneja la coneccion
     * @param IP
     */
    public void HandleConnection(String IP) {

        model = Status.getModel();

        if (model.getSize() > 100) {

            modelSize = 100;
        }
        else {

            modelSize = model.getSize();
        }

        StatusString = new String[modelSize + 1];

        StatusString[0] = "Esperando Mensaje del Cliente...";

        for (int i = 1; i < StatusString.length; i++) {

            StatusString[i] = model.getElementAt(i - 1).toString();
        }

        Status.setListData(StatusString);


        while (true) {

            try {

                Socket socket = server.accept();
                new MessageHandler(socket, IP, Status);
            }
            catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }
}
